package org.zk.dubbo_provider.config;

import com.github.pagehelper.PageInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.zk.dubbo_provider.utils.RedisUtil;
import org.zk.entity.Broadcast;
import org.zk.entity.Course;
import org.zk.entity.Lession;
import org.zk.entity.User;

import javax.annotation.Resource;
import java.util.Map;

@Aspect
@Component
public class AopConfig {
    @Resource
    private RedisUtil redisUtil;

    private long time = 30*60;
    /*------------------------------分页用的切点开始----------------------------------*/
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.*Impl.get*Page(..))")
    public void Log(){}
    /*------------------------------分页用的切点结束----------------------------------*/

    /*-------------------------------分页缓存开始---------------------------------*/
    //这个方法主要用于分页的缓存
    @Around("Log()")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object value = null;
        //获得类的名
        String targetName = joinPoint.getTarget().toString().split("@")[0];
        //获得方法名
        String methodName = joinPoint.getSignature().getName();
        //获得参数
        Object[] objects = joinPoint.getArgs();
        //调用下面的方法拼成key
        final String key = getCacheKey(targetName, methodName);
        //调用下面的方法拼成item
        final String item = getCacheItem(objects);
        //判断缓存中是否存在，如果存在取出返回，不存在则继续下一步
        if (redisUtil.hHasKey(key, item)) {
            return redisUtil.hget(key, item);
        }
        try {
            value = joinPoint.proceed();//得到从数据库查询到的数据
            PageInfo pageInfo = (PageInfo) value;//将获得的数据强转为PageInfo，
            if (pageInfo.getList().size() != 0) {
                final String tkey = key;
                final String titem = item;
                final Object tval = value;
                //开启独立的线程，开始写入
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        redisUtil.hset(tkey, titem, tval, time);//调用缓存工具类，进行缓存
                    }
                }).start();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return value;
    }
    /**
     * 构造key 用于hash缓存的key的构造
     * @param targetName 类名
     * @param methodName 方法名
     * @return
     */
    public String getCacheKey(String targetName, String methodName) {
        StringBuffer sbu = new StringBuffer();
        sbu.append(targetName).append("_").append(methodName);
        return sbu.toString();
    }
    /**
     * 构造item ，用于hash缓存item的构造
     * @param objects
     * @return
     */
    public String getCacheItem(Object[] objects){
        Map<String, Object> map = (Map<String, Object>) objects[0];
        String pageSize = String.valueOf(map.get("pageSize"));//分页用的每页大小
        String pageNum = String.valueOf(map.get("pageNum"));//分页用得当前页
        String seetext = String.valueOf(map.get("seetext"));//模糊查询的条件
        String datemin = String.valueOf(map.get("start"));//模糊查询的开始时间
        String datemax = String.valueOf(map.get("end"));//模糊查询的结束时间
        String teacherid = String.valueOf(map.get("teacherid"));//根据 teacherid
        String teacherId = String.valueOf(map.get("teacherId"));
        String courseId = String.valueOf(map.get("courseId"));//课程ID
        String status = String.valueOf(map.get("status"));//提现是否处理
        String currTime = String.valueOf(map.get("currTime"));
        String choise = String.valueOf(map.get("choise"));
        String id = String.valueOf(map.get("id"));
        String typeId = String.valueOf(map.get("typeId"));
        String userName = String.valueOf(map.get("userName"));
        String userGender = String.valueOf(map.get("userGender"));
        String userPhone = String.valueOf(map.get("userPhone"));
        String userVip = String.valueOf(map.get("userVip"));

        User user = (User) map.get("user");
        Lession lession = (Lession) map.get("lession");
        Course course = (Course) map.get("course");
        Broadcast broadcast = (Broadcast) map.get("broadcast");

        String item = pageNum + ":" + pageSize ;

        if (!seetext.equals("null") && !seetext.equals("")){
            System.out.println("--"+seetext+"--");
            item = item + ":" + seetext;
        }
        if (!datemax.equals("null") && !datemax.equals("")){
            item = item + ":" + datemax;
        }
        if (!datemin.equals("null") && !datemin.equals("")){
            item = item + ":" + datemin;
        }
        if (!teacherid.equals("null") && !teacherid.equals("")){
            item = item + ":" + teacherid;
        }
        if (!teacherId.equals("null") && !teacherId.equals("")){
            item = item + ":" + teacherId;
        }
        if (lession != null){
            String lessionState = lession.getLessionState();//课节的审核状态
            item = item + ":" + lessionState;
        }
        if (!courseId.equals("null") && !courseId.equals("")){
            item = item + ":" + courseId;
        }
        if (course != null){
            String courseFlag = course.getCourseFlag();//课程的审核状态
            item = item + ":" + courseFlag;
        }
        if (!status.equals("null") && !status.equals("")){
            item = item + ":" + status;
        }
        if (!currTime.equals("null") && !currTime.equals("")){
            item = item + ":" + currTime;
        }
        if (!choise.equals("null") && !choise.equals("")){
            item = item + ":" + choise;
        }
        if (broadcast != null){
            String tid = String.valueOf(broadcast.getTeacher().getTeacherId());
            String flag = String.valueOf(broadcast.getBroadcastFlag());
            String bid = String.valueOf(broadcast.getBroadcastId());
            if (tid.equals("")){
                item = item + ":" + tid;
            }
            if (flag.equals("")){
                item = item + ":" + flag;
            }
            if (bid.equals("")){
                item = item + ":" + bid;
            }

        }
        if (!id.equals("null") && !id.equals("")){
            item = item + ":" + id;
        }
        if (!typeId.equals("null") && !typeId.equals("")){
            item = item + ":" + typeId;
        }
        if (!userName.equals("null") && !userName.equals("")){
            item = item + ":" + userName;
        }
        if (!userGender.equals("null") && !userGender.equals("")){
            item = item + ":" + userGender;
        }
        if (!userPhone.equals("null") && !userPhone.equals("")){
            item = item + ":" + userPhone;
        }
        if (!userVip.equals("null") && !userVip.equals("")){
            item = item + ":" + userVip;
        }
        if (user != null){
            String getUserVip = String.valueOf(user.getUserVip());
            item = item + ":" + getUserVip;
        }
        return item;
    }
    /*-------------------------------分页缓存结束---------------------------------*/
    /*-------------------------------分页缓存刷新开始---------------------------------*/
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.UserServiceImpl.insertUser*(..))")
    public void userinsertLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.UserServiceImpl.updateUserEdit(..))")
    public void updateUserEditLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.UserServiceImpl.updateUser(..))")
    public void updateUserLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.insertTeacher*(..))")
    public void teacherinsertLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.updateTeacher*(..))")
    public void teacherupdateLog(){}
    @After("userinsertLog()||delUserByIdLog()||updateTeacherferLog()||updateUserEditLog()||updateUserLog()||teacherinsertLog()||teacherupdateLog()")
    public void delgetTeacherBillPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getTeacherBillPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getTeacherBillPage");
        }
    }

    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.AdminBillServiceImpl.insertTeacherBill(..))")
    public void insertTeacherBillLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.AdminBillServiceImpl.updateTeacherfer(..))")
    public void updateTeacherferLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherBillServiceImpl.delTeacherBill(..))")
    public void delTeacherBillLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.updateTeacherMoney(..))")
    public void updateTeacherMoneyLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.insertTixianTeacher(..))")
    public void insertTixianTeacherLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.insertTeacherYes(..))")
    public void insertTeacherYesLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.insertTeacherMoneyByCourse(..))")
    public void insertTeacherMoneyByCourseLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.CourseServiceImpl.insertCourse(..))")
    public void insertCourseLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.OrderServiceImpl.insert*Order(..))")
    public void insertOrderLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.OrderServiceImpl.delOrder(..))")
    public void delOrderLog(){}
    @After("insertTeacherBillLog()||updateTeacherferLog()||delTeacherBillLog()||updateTeacherMoneyLog()||insertTixianTeacherLog()||insertTeacherYesLog()||insertTeacherMoneyByCourseLog()||insertCourseLog()||insertOrderLog()||delOrderLog()")
    public void delgetPayoffToTeacherPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getPayoffToTeacherPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getPayoffToTeacherPage");
        }
    }
    @After("insertOrderLog()||delOrderLog()||insertCourseLog()||updateTeacherferLog()||delTeacherBillLog()||insertTeacherBillLog()")
    public void delgetTeacherbillByTeacherid0Page(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getTeacherbillByTeacherid0Page")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getTeacherbillByTeacherid0Page");
        }
    }
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TixianServiceImpl.updateTixianStatusTeacher(..))")
    public void updateTixianStatusTeacherLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.AdminBillServiceImpl.delOutAdminBill(..))")
    public void delOutAdminBillLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TixianServiceImpl.delTixian(..))")
    public void delTixianLog(){}
    @After("updateTixianStatusTeacherLog()||delOutAdminBillLog()||delTixianLog()")
    public void delgetOutPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getOutPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getOutPage");
        }
    }
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.AdminBillServiceImpl.delInAdminBill(..))")
    public void delInAdminBillLog(){}
    @After("updateTixianStatusTeacherLog()||delInAdminBillLog()||delTixianLog()")
    public void delgetInPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getInPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.AdminBillServiceImpl_getInPage");
        }
    }

    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.BroadcastServiceImpl.insertBroadcast(..))")
    public void insertBroadcastLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.BroadcastServiceImpl.deleteBroadcastById(..))")
    public void deleteBroadcastByIdLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.BroadcastServiceImpl.update*(..))")
    public void upLog(){}
    @After("insertBroadcastLog()||deleteBroadcastByIdLog()||upLog()")
    public void delgetBroadcastByIdByPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.BroadcastServiceImpl_getBroadcastByIdByPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.BroadcastServiceImpl_getBroadcastByIdByPage");
        }
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.BroadcastServiceImpl_getAllBroadcastPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.BroadcastServiceImpl_getAllBroadcastPage");
        }
    }

    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.CommentServiceImpl.addComm(..))")
    public void addCommLog(){}
    @After("addCommLog()")
    public void delgetqueryCommentPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.CommentServiceImpl_getqueryCommentPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.CommentServiceImpl_getqueryCommentPage");
        }
    }
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.CourseServiceImpl.update*(..))")
    public void updateCourseLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.CourseServiceImpl.delCourseById(..))")
    public void delCourseByIdLog(){}
    @After("insertCourseLog()||updateCourseLog()||delCourseByIdLog()||delTypeSubjectCourseByTid()")
    public void delgetqueryCourseByTidPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.CourseServiceImpl_getqueryCourseByTidPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.CourseServiceImpl_getqueryCourseByTidPage");
        }
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.CourseServiceImpl_getAllCoursePage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.CourseServiceImpl_getAllCoursePage");
        }
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.CourseServiceImpl_getAllCoursesPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.CourseServiceImpl_getAllCoursesPage");
        }
    }

    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.LessionServiceImpl.addLession(..))")
    public void addLessionLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.LessionServiceImpl.updateLessionState(..))")
    public void updateLessionStateLog(){}
    @After("addLessionLog()||updateLessionStateLog()")
    public void delgetAllLessionPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.LessionServiceImpl_getAllLessionPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.LessionServiceImpl_getAllLessionPage");
        }
    }
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.SubjectServiceImpl.insertSubject(..))")
    public void insertSubjectLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.SubjectServiceImpl.delSubjectCourseBySid(..))")
    public void delSubLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.SubjectServiceImpl.updateSubjectByTid(..))")
    public void updateSubjectByTidLog(){}
    @After("insertSubjectLog()||delSubLog()||updateSubjectByTidLog()||delTypeSubjectCourseByTid()")
    public void delgetAllSubjectsPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.SubjectServiceImpl_getAllSubjectsPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.SubjectServiceImpl_getAllSubjectsPage");
        }
    }
    @After("insertTeacherBillLog()||updateTeacherferLog()||delTeacherBillLog()")
    public void delgetTeacherbillsPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.TeacherBillServiceImpl_getTeacherbillsPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.TeacherBillServiceImpl_getTeacherbillsPage");
        }
    }
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TeacherServiceImpl.insertTixian(..))")
    public void insertTixianLog(){}
    @After("updateTixianStatusTeacherLog()||delTixianLog()||insertTixianLog()")
    public void delgetApplyTixianPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.TixianServiceImpl_getApplyTixianPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.TixianServiceImpl_getApplyTixianPage");
        }
    }
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TypeServiceImpl.addType(..))")
    public void addTypeLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TypeServiceImpl.updateType(..))")
    public void updateTypeLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.TypeServiceImpl.delTypeSubjectCourseByTid(..))")
    public void delTypeSubjectCourseByTid(){}
    @After("addTypeLog()||updateTypeLog()||delTypeSubjectCourseByTid()")
    public void delgetAllTypesPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.TypeServiceImpl_getAllTypesPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.TypeServiceImpl_getAllTypesPage");
        }
    }

    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.UserServiceImpl.updateUser*(..))")
    public void userupdateLog(){}
    @Pointcut("execution(public * org.zk.dubbo_provider.service.impl.UserServiceImpl.delUserById(..))")
    public void delUserByIdLog(){}
    @After("userinsertLog()||userupdateLog()||delUserByIdLog()")
    public void delgetlookAllUserPage(){
        if (redisUtil.hasKey("org.zk.dubbo_provider.service.impl.UserServiceImpl_getlookAllUserPage")){
            redisUtil.del("org.zk.dubbo_provider.service.impl.UserServiceImpl_getlookAllUserPage");
        }
    }

    /*-------------------------------分页缓存刷新结束---------------------------------*/
















}
